#include <profiling.h>
#include <spu_mfcio.h>
#include <stdio.h>


#define TIMEBASE 79800000.0 // cat /proc/cpuinfo | grep -w timebase
#define StartTimer(ts) {spu_write_decrementer(0xffffffff); ts=spu_read_decrementer();}
#define StopTimer(te) {te -= spu_read_decrementer();}
#define PrintTimer(id, msg, te) {printf("\tSPU #%i:timer:%s: %f(sec)\n", id, msg, (1 / (double)TIMEBASE) * te);}


double profile_dma; double profile_dma_total;
double profile_exec; double profile_exec_total;

void profiling_setup()
{
	profile_dma=0; profile_dma_total=0;
	profile_exec=0; profile_exec_total=0;
}

void profiling_print(int spe_id)
{
	PrintTimer(spe_id, " EXEC ", profile_exec_total);
	PrintTimer(spe_id, " DMA ", profile_dma_total);
}

void profiling_dma_start()
{
	StartTimer(profile_dma);
}

void profiling_dma_stop()
{
	StopTimer(profile_dma);
	profile_dma_total += profile_dma;
}


void profiling_exec_start()
{
	StartTimer(profile_exec);
}

void profiling_exec_stop()
{
	StopTimer(profile_exec);
	profile_exec_total += profile_exec;
}


